import { createRouter, createWebHistory } from 'vue-router'

import routes from './routes'
import useUserStore from '@/store/user'

const router = createRouter({
    history: createWebHistory(),
    routes,
})

router.beforeEach((to, from) => {
    const store = useUserStore()

    if (to.meta.permission) {
        if (!store.token) {
            return {
                path: '/login',
                query: {
                    redirect: to.path,
                },
            }
        }
    }
})
export default router
